<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class GpostsController extends Controller
{
    // 商品帖子列表
    public function getIndex(Request $request)
    {
      // dd($request->all());
      // 判断是否有查询条件
      if(!empty($request->input('keywords'))){
        $gposts = DB::table('gposts')->select(DB::raw('gposts.*,users.username'))->leftjoin('users','gposts.gpuid','=','users.uid')->where('gposts.gptitle','like','%'.$request->input('keywords').'%')->orWhere('gposts.gpoint','like','%'.$request->input('keywords').'%')->orWhere('users.username','like','%'.$request->input('keywords').'%')->paginate(15);
      }else{
        $gposts = DB::table('gposts')->select(DB::raw('gposts.*,users.username'))->leftjoin('users','gposts.gpuid','=','users.uid')->paginate(15);
      }

      // $gposts = DB::table('gposts')->select(DB::raw('gposts.*,users.username'))->leftjoin('users','gposts.gpuid','=','users.uid')->paginate(15);

      $list = $request->all();  //获取条件参数,用于下一页时传参

      return view('admin.gposts.index',['list'=>$list,'gposts'=>$gposts]);  //解析模板 分配数据
    }

    // 用于查询商品帖子详情信息等(ajax)
    public function getDetailcheck()
    {
      $gpid = $_GET['gpid']; //获取商品帖子gid

      $gpcomments = DB::select('select gpcomments.*,users.username from gpcomments left join users on gpcomments.gpcuid = users.uid where gpcomments.gpid = '.$gpid.' order by gpcomments.gpctime desc');
      
      echo json_encode($gpcomments); 
    }

    // 添加商品帖子
    public function getAdd()
    {
    	//解析添加商品模版
    	return view('admin.gposts.add');
    }

    // 处理商品帖子添加
    public function postDoadd(Request $request)
    {
      $data = $request->except('_token');

      $data['gpic'] = $this->upload($request,'gpic');  //调用函数处理图片
      
      $data['gpctime'] = date('Y-m-d H:i:s',time());  // 添加时间

      $data['gputime'] = '暂未修改';

      $res = DB::table('gposts')->insert($data);
      if($res){
        return redirect('/admin/gposts/index')->with('success','发帖成功');
      }else{
        return back()->with('error','发帖失败');
      }
    }

    // 修改帖子信息
    public function getEdit(Request $request)
    {
        $gpid = $request->gpid;  //获取帖子
        $gposts = DB::table('gposts')->where('gpid',$gpid)->first(); 
        // dd($gposts);
        return view('admin.gposts.edit',['gposts'=>$gposts]);
    }

    // 处理修改帖子信息
    public function postDoedit(Request $request)
    {   
        $data = $request->except('_token','gpuid','gpid','oldgpic');

        // 修改时间
        $data['gputime'] = date('Y-m-d H:i:s',time());

        // 判断是否修改图片
        if(empty($request->gpic)){
            $data['gpic'] = $request->oldgpic;
        }else{
            $data['gpic'] = $this->upload($request,'gpic');  //调用函数处理图片
        }

        // dd($data);
        $res = DB::table('gposts')->where('gpid',$request->gpid)->update($data);
        if($res){
            return redirect('/admin/gposts/index')->with('success','修改帖子成功');
        }else{
            return back()->with('error','修改帖子失败');
        }
    }

    // 修改帖子状态 AJAX
    public function getCstatus()
    {   
        $gpstatus = $_GET['gpstatus'];
        $gpid = $_GET['gpid'];
        // 修改状态
        if($gpstatus == '审核中'){
            $res = DB::table('gposts')->where('gpid','=',$gpid)->update(['gpstatus' => '已通过']);
        }else{
            $res = DB::table('gposts')->where('gpid','=',$gpid)->update(['gpstatus' => '审核中']);
        }
        
        $status = DB::table('gposts')->select('gpstatus')->where('gpid','=',$gpid)->first()->gpstatus;
        // dd($status);
        echo json_encode($status);
    }

     // 封装一个方法用来处理分类图片上传
    private function upload($request,$file)
    {
        if($request->hasFile($file)){
            $suffix = $request->file($file)->getClientOriginalExtension(); // 获取文件后缀名
            $name = md5(time()).rand(0,99999);  //生成一个随机文件名
            $request->file($file)->move('./uploads/gposts/',$name.'.'.$suffix);  //移动
            return '/uploads/gposts/'.$name.'.'.$suffix;  //返回路径信息
        }
    }
}
